﻿using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public partial class FuncTree : UIPanel
    {
        FuncTreeManager funcTreeManager;
        private long projectId;
        private Func currFunc;

        public delegate void OnSelectedFuncChangeHandler(Func selectedFunc);

        public event OnSelectedFuncChangeHandler OnSelectedFuncChange;

        public FuncTree()
        {
            InitializeComponent();
            funcTreeManager = new FuncTreeManager(this.FindForm());
        }

        public FuncTree(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            funcTreeManager = new FuncTreeManager(this.FindForm());
        }

        public void SetCurrProject(long projectId)
        {
            this.projectId = projectId;
            funcTreeManager.LoadFuncTree(projectId, treeFuncs, -1);
        }

        private void btnAddSblingFunc_Click(object sender, EventArgs e)
        {
            if (projectId == 0)
            {
                return;
            }
            Func selectedFunc = null;
            if (treeFuncs.SelectedNode != null)
            {
                selectedFunc = treeFuncs.SelectedNode.Tag as Func;
            }

            funcTreeManager.AddFunc(treeFuncs, projectId, selectedFunc, 1);
        }

        private void btnAddChildFunc_Click(object sender, EventArgs e)
        {
            if (projectId == 0)
            {
                return;
            }
            Func selectedFunc = null;
            if (treeFuncs.SelectedNode != null)
            {
                selectedFunc = treeFuncs.SelectedNode.Tag as Func;
            }

            funcTreeManager.AddFunc(treeFuncs, projectId, selectedFunc, 2);
        }

        private void btnEditFunc_Click(object sender, EventArgs e)
        {
            Func selectedFunc = null;
            if (treeFuncs.SelectedNode != null)
            {
                selectedFunc = treeFuncs.SelectedNode.Tag as Func;
                funcTreeManager.EditFunc(treeFuncs, selectedFunc);
            }
        }

        private void btnDelFunc_Click(object sender, EventArgs e)
        {
            Func selectedFunc = null;
            if (treeFuncs.SelectedNode != null)
            {
                if (this.GetParentForm().ShowAskDialog("确定要删除节点[" + treeFuncs.SelectedNode.Text + "]吗？"))
                {
                    selectedFunc = treeFuncs.SelectedNode.Tag as Func;
                    funcTreeManager.DeleteFunc(treeFuncs, selectedFunc);
                }
            }
        }

        private void btnReloadFuncs_Click(object sender, EventArgs e)
        {
            if (projectId == 0) return;
            funcTreeManager.LoadFuncTree(projectId, treeFuncs, -1);
        }

        private void treeFuncs_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            Func func = e.Node.Tag as Func;
            if (func != null)
            {
                chgCurrFunc(func);
            }
        }

        private void chgCurrFunc(Func func)
        {
            if (func == null) return;

            if (func.FuncType == 1)
            {
                if (currFunc == null || func.Id != currFunc.Id)
                {
                    if(OnSelectedFuncChange != null) OnSelectedFuncChange(func);
                }
            }
            currFunc = func;
        }

        private void btnUploadPdmFile_Click(object sender, EventArgs e)
        {
            if (projectId == 0)
            {
                return;
            }
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog1.FileName;
                var uploadFileInfo = ProjectHandler.uploadPdmFile(fileName);
                var project = ProjectHandler.getProject(projectId);
                if (uploadFileInfo != null)
                {
                    project.PdmFileName = uploadFileInfo.OrgFileName;
                    project.PdmFilePath = uploadFileInfo.NewFileName;
                    ProjectHandler.saveProject(project);
                    (this.FindForm() as UIForm).ShowSuccessTip("pdm文件已上传成功");
                }
            }
        }
    }
}
